<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Kabupaten_model extends MY_Model {

    function __construct() {
        parent::__construct("kabupaten", "code", Array(
            "provinsi_code" => "Provinsi_model"
        ));
    }

    public function findByName($name, $fetchForeign = false, $depth = 1) {
        $where = "UCASE(REPLACE(display_name, ' ', '')) LIKE UCASE(REPLACE('" . $name . "', ' ', ''))";
        $result = $this->read($where, $fetchForeign, $depth);
        $resObj = NULL;
        if (count($result) > 0) {
            $resObj = $result[0];
        }
        return $resObj;
    }

    public function readSummarized($where = Array(), $fetchForeign = false, $depth = 1, $page = -1, $limit = 10) {
        $results = parent::read($where, $fetchForeign, $depth, $page, $limit);

        foreach ($results as $result) {
            $AHH = "n/a";
            $IPM = "n/a";

            $this->load->database("data_ps");
            $this->db->select("data.value");
            $this->db->from("data, form, kabupaten_data, indicator");
            $this->db->where("kabupaten_data.data_id = data.id");
            $this->db->where("data.indicator_id = indicator.id");
            $this->db->where("data.form_id = form.id");
            $this->db->where("data.is_deleted", false);
            $this->db->where("form.is_approved", true);
            $this->db->where("form.is_deleted", false);
            $this->db->where("indicator.name", "AHH");
            $this->db->where("kabupaten_data.kabupaten_code", $result->code);
            $this->db->order_by("data.year", "desc");
            $this->db->order_by("data.month", "desc");
            $this->db->limit(1, 0);
            $query = $this->db->get();
            $rowval = $query->row();
            if ($rowval != NULL) {
                $AHH = $rowval->value;
            }
            $this->load->database("data_ps");
            $this->db->select("data.value");
            $this->db->from("data, form, kabupaten_data, indicator");
            $this->db->where("kabupaten_data.data_id = data.id");
            $this->db->where("data.indicator_id = indicator.id");
            $this->db->where("data.form_id = form.id");
            $this->db->where("data.is_deleted", false);
            $this->db->where("form.is_approved", true);
            $this->db->where("form.is_deleted", false);
            $this->db->where("indicator.name", "IPM");
            $this->db->where("kabupaten_data.kabupaten_code", $result->code);
            $this->db->order_by("data.year", "desc");
            $this->db->order_by("data.month", "desc");
            $this->db->limit(1, 0);
            $query = $this->db->get();
            $rowval = $query->row();
            if ($rowval != NULL) {
                $IPM = $rowval->value;
            }

            $summary = "";
            $summary .= "AHH : " . $AHH . " ";
            $summary .= "IPM : " . $IPM;
            $result->generated_summary = $summary;
        }

        return $results;
    }

}